const {
  LinValidator,
  Rule
} = require('../core/lin-validator.js');

const User = require('../models/User.js');

const {
  loginType
} = require('./enum.js');

// 校验参数是否为正整数
class ValidationInteger extends LinValidator {
  constructor() {
    super();
    this.id = [
      // 这里可以添加多个校验规则，但是规则是且的关系
      // 三个参数：第一个参数：需要满足的规则，第二个参数：提示信息，第三个参数：可选参数
      new Rule('isEmpty', '不能为空', {
        min: 1
      })
      // new Rule ('isNotEmpty', '必须传入参数')
    ]
  }
}

class ValidationRegister extends LinValidator {
  constructor() {
    super();
    this.nickname = [
      new Rule('isLength', '账号长度需在6-20', {
        min: 6,
        max: 20
      })
    ];
    this.password = [
      new Rule('isLength', '密码长度需在6-20', {
        min: 6,
        max: 20
      })
    ];
    this.email = [
      new Rule('isEmail', '邮箱格式不正确')
    ];
  }

  async isEmail (vals) {
    const abc = await User.findOne({
      where: '123456@qq.com'
    });
  }
};

class isToken extends LinValidator {
  constructor() {
    super();
    // this.account = [
    //   new Rule('isOptional')
    // ];
    this.password = [
      new Rule('isOptional'),
      new Rule('isLength', '长度需在6-20之间', {
        min: 6,
        max: 20
      })
    ];
    this.type = [
      new Rule('isIn', '不是合法值', loginType)
    ]
  }

  // isType(vals) {
  //   console.log('abc')
  //   throw new Error('type为必填参数');
  //   if (!vals.body.type) {
  //   }
  // }
};

class NotEmptyValidator extends LinValidator {
  constructor() {
    super();
    this.token = [
      new Rule('isLength', '不允许为空', {
        min: 1
      })
    ]
  }
};

class likeValidator extends LinValidator {
  constructor() {
    super();
  }
};

class ClassicValidator extends LinValidator {
  constructor() {
    super();
    this.type = [];
  }
};

class searchValidator extends LinValidator {
  constructor() {
    super();
    this.q = [
      new Rule('isLength', '搜索关键词不能为空', {min:1, max: 16})
    ];
    this.start = [
      new Rule('isInt', 'start不符合规范', {min:0, max: 60000}),
      new Rule('isOptional', '', 0)
    ];
    this.count = [
      new Rule('is', 'count不符合规范', {min:1, max: 20}),
      new Rule('isOptional', '', 20)
    ];
  }
};


module.exports = {
  ValidationInteger,
  ValidationRegister,
  isToken,
  NotEmptyValidator,
  likeValidator,
  ClassicValidator,
  searchValidator
}